import http from '../utils/httpRequest.js'
import config from "../../config.js"

let orderListTest = genTestOrderList()

// 订单列表
export function getOrderList(params) {
    params.path = `/order/list?pageNo=${params.pageNo}&pageSize=${params.pageSize}&orderStatus=${JSON.stringify(params.orderStatus)}`
    let list = orderListTest.filter(order => {
        if (params.orderStatus === config.orderStatus.Refunding || params.orderStatus === config.orderStatus.Refunded) {
            return order.orderStatus === config.orderStatus.Refunding || order.orderStatus === config.orderStatus.Refunded || order.refundCount > 0
        }
        return params.orderStatus === 0 || order.orderStatus === params.orderStatus
    })
    params.testData = {
        total: list.length,
        list: list.slice((params.pageNo - 1) * params.pageSize, params.pageNo * params.pageSize)
    }
    http(params)
}

// 改变订单状态
export function updateOrderStatus(params) {
    if (params.refundGoodsMap == null)
        params.refundGoodsMap = []
    http({
        ...params,
        method: "put",
        path: `/order/status`,
        data: {
            orderNo: params.orderNo,
            command: params.command,
            refundReason: params.refundReason,
            refundGoodsMap: params.refundGoodsMap
        },
        testData: function () {
            let index = orderListTest.findIndex(e => {
                return e.orderNo === params.orderNo
            })
            let order = orderListTest[index]
            if (params.command === 1) { // 取消并删除未付款订单
                orderListTest.splice(index, 1)
            } else if (params.command === 2) { // 发货
                orderListTest[index].orderStatus = config.orderStatus.WaitTake
            } else if (params.command === 3) { // 退款
                order.refundCount = 0
                for (let i = 0; i < order.goodsList.length; i++) {
                    if (params.refundGoodsMap[i] === true) {
                        order.goodsList[i].refundNo = order.orderNo + "-" + Math.random()
                        order.goodsList[i].refundReason = params.refundReason
                        order.totalPrice -= order.goodsList[i].price
                    }
                    if (order.goodsList[i].refundNo)
                        order.refundCount += 1
                }
                if (order.refundCount === order.goodsList.length) { // 全部商品都要退款
                    order.orderStatus = config.orderStatus.Refunding
                }
            }
            return true
        },
    })
}

// 打印订单小票
export function printOrderInfo(params) {
    params.method = "POST"
    params.path = "/order/print/" + params.orderNo
    http(params)
}

// 随机生成测试数据
function genTestOrderList() {
    let list = []
    let statusList = [config.orderStatus.NotPay, config.orderStatus.WaitSend, config.orderStatus.WaitTake, config.orderStatus.Finished]
    let timestamp = new Date().getTime()
    let goodsAvatar = "https://img0.baidu.com/it/u=3074490370,3840344350&fm=26&fmt=auto"
    let randInt = function (min, max) {
        return min + Math.floor(Math.random() * max);
    }
    for (let i = 0; i < 300; i++) {
        timestamp += randInt(1, 3600 * 24 * 1000)
        let order = {
            orderNo: timestamp + randInt(1, 100000000),
            userId: 1,
            orderStatus: statusList[randInt(0, statusList.length)],
            wxPayTransactionId: "4200001338202112" + randInt(1, 100000000),
            takeType: 1,
            addressDetail: "四川农业大学2区15舍309",
            goodsList: [],
            refundCount: 0,
            totalPrice: 0,
            payPrice: 0,
            extraInfo: "extraInfo-sxxx",
            createTime: timestamp,
            payTime: timestamp,
            finishTime: "",
            userPhone: "17058179046",
            receiver: "陈亚如"
        }
        // 订单商品列表
        for (let i = 0; i < randInt(1, 5); i++) {
            let needRefund = order.orderStatus !== config.orderStatus.NotPay && (randInt(1, 100) < 20)
            order.goodsList.push({
                goodsId: i,
                price: randInt(1, 1000),
                count: randInt(1, 3),
                propertyMap: {
                    "属性1": "xxx1",
                    "属性2": "yyy1",
                },
                name: "goods-" + randInt(0, 5),
                image: goodsAvatar,
                refundNo: needRefund ? "refundNoxxx" : "", // 退款号（如果该商品要退款的话）
                refunded: false,
                refundReason: needRefund ? "库存不够" : "",
            })
            order.totalPrice += order.goodsList[i].price * order.goodsList[i].count
            if (needRefund) {
                order.refundCount++
            } else {
                order.payPrice += order.goodsList[i].price * order.goodsList[i].count
            }
        }
        order.payPrice = (order.orderStatus === config.orderStatus.NotPay ? 0 : order.payPrice)
        if (order.refundCount === order.goodsList.length) order.orderStatus = config.orderStatus.Refunding
        list.push(order)
    }
    return list
}
